<?php
ob_start();
require_once('../inc/config.inc');
	//Connect to mysql server
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	//Select database
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
	if(isset($_GET['key'])){
		$key=$_GET['key'];
		$qry=mysql_query("SELECT * FROM `users_no_confirm` WHERE `key` ='".$key."'");
		if($qry){
		if(mysql_num_rows($qry)==1){
			$row=mysql_fetch_array($qry);
			$insert=mysql_query("INSERT INTO `users`(`id`,`firstname`,`lastname`,`email`,`password`,`businessname`,`url`,`category`) VALUES(null,'".$row['firstname']."','".$row['lastname']."','".$row['email']."','".$row['password']."','".$row['businessname']."','".$row['url']."','".$row['category']."')");
			if ($insert) {
			$delete=mysql_query("DELETE FROM `users_no_confirm` WHERE `key` = '".$key."' ");
			if($delete){
				header("Location: ../login.php?confirm=success");
			}
			else{
				echo "Error occured: " . mysql_error();
			}
			}
			else{
				echo mysql_error();
			}
			
		}
		else{
			echo "Your Link is invalid, please try again.";
		}
	}
	else{
		echo "NOT: " . mysql_error();
	}
	}
	else{
		header("Location: ../register.php");
	}